package com.sky.controller.admin;

import com.sky.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

@RestController("adminShopController")
@RequestMapping("/admin/shop")
@Api(tags = "后台-商铺管理")
@Slf4j
public class ShopController {
    public static final String KEY = "SHOP_STATUS";

    @Autowired
    private RedisTemplate redisTemplate;
//    @Autowired
//    private ShopService shopService;

    @PutMapping("/{status}")
    @ApiOperation(value = "设置商铺状态", notes = "设置商铺状态")
    public Result setShopStatus(@PathVariable("status") Integer status) {
        log.info("设置商铺状态：{}", status==1? "上线" : "下线");
        try {
            redisTemplate.opsForValue().set(KEY, status);
        } catch (Exception e) {
            log.error("设置商铺状态失败：{}", e.getMessage());
            return Result.error("设置商铺状态失败");
        }

        //在数据库设置商铺状态
        return Result.success();

    }

    //获取商铺状态
    @GetMapping("/status")
    @ApiOperation(value = "获取商铺状态", notes = "获取商铺状态")
    public Result<Integer> getShopStatus() {
        Integer status = (Integer) redisTemplate.opsForValue().get(KEY);
        log.info("获取到的商铺状态：{}", status==1? "上线" : "下线");
        return Result.success(status);
    }
}
